package com.water.camera.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.water.camera.activity.BatchSuccessAct;
import com.water.camera.databinding.ItemAlbum2Binding;

import java.io.File;
import java.util.List;

public class BatchSuccAdapter extends RecyclerView.Adapter {

    List<String> list;
    BatchSuccessAct act;

    public BatchSuccAdapter(BatchSuccessAct act, List<String> list) {
        this.list = list;
        this.act = act;
    }

    public void update(List<String> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemAlbum2Binding binding = ItemAlbum2Binding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ImgHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ImgHolder) {
            ((ImgHolder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class ImgHolder extends RecyclerView.ViewHolder {
        ItemAlbum2Binding binding;

        public ImgHolder(ItemAlbum2Binding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            binding.frame.setRatio(1.33f);
            Glide.with(itemView).load(new File(list.get(p))).into(binding.ivCover);
            itemView.setOnClickListener(v -> act.toPreview(p));
        }
    }
}
